﻿using MyProduce.MyProduce.ProduceDto;
using MyProduce.Produce;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace MyProduce.MyProduce
{
    public class SeedweaningAppService : ApplicationService, ISeedweaningAppService
    {
        private readonly IRepository<Seedweaning, int> _seedbirthRepository;
        public SeedweaningAppService(IRepository<Seedweaning, int> seedbirthRepository)
        {
            _seedbirthRepository = seedbirthRepository;

        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Aid"></param>
        /// <returns></returns>

        public async Task DeleteAsync(int Aid)
        {
            await _seedbirthRepository.DeleteAsync(Aid);
        }

        /// <summary>
        /// 显示种母断奶列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<SeedweaningDto>> GetListAsync(string? Overbit)
        {
            var result = await _seedbirthRepository.GetListAsync();
            if (Overbit!= null)
            {
                result = result.Where(x => x.Overbit.Contains(Overbit)).ToList();
            }
            return ObjectMapper.Map<List<Seedweaning>, List<SeedweaningDto>>(result);
        }
        /// <summary>
        /// 显示重量
        /// </summary>
        /// <returns></returns>
        public async Task<List<weightDto>> GetwigAsync()
        {
            var result = await _seedbirthRepository.GetListAsync();
            var list = (from x in result
                        select new weightDto
                        {
                             Weight= x.weightlitter
                        }
                      );
            return list.ToList();
         }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="eid"></param>
        /// <returns></returns>
        public  async Task<SeedweaningDto> GetSeedweaningFindaa(int eid)
        {
            var result = await _seedbirthRepository.GetAsync(eid);
            return ObjectMapper.Map<Seedweaning, SeedweaningDto>(result);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="appearanceDTO"></param>
        /// <returns></returns>
        public  async Task<EarResult<SeedweaningDto>> SeedweaningAdds(SeedweaningDto appearanceDTO)
        {
            Seedweaning ewebreding = new Seedweaning();


            ewebreding.Transfertobuilding=appearanceDTO.Transfertobuilding;
            ewebreding.Variety=appearanceDTO.Variety;
            ewebreding.Cottage=appearanceDTO.Cottage;
            ewebreding.Field=appearanceDTO.Field;
            ewebreding.Gender=appearanceDTO.Gender;
            ewebreding.Overbit=appearanceDTO.Overbit;
            ewebreding.Drivefield=appearanceDTO.Drivefield;
            ewebreding.Breastfeedingdays=appearanceDTO.Breastfeedingdays;
            ewebreding.Weaningdays=appearanceDTO.Weaningdays;
            ewebreding.weightlitter=appearanceDTO.weightlitter;
            ewebreding.Weaningdate=appearanceDTO.Weaningdate;
            var result = await _seedbirthRepository.InsertAsync(ewebreding);
            var res = ObjectMapper.Map<Seedweaning, SeedweaningDto>(result);
            if (res != null)
            {
                return new EarResult<SeedweaningDto>() { Code = 200, Message = "添加成功" };
            }
            else
            {
                return new EarResult<SeedweaningDto>() { Code = 500, Message = "添加失败" };
            }
        }

        public async Task<EarResult<SeedweaningDto>> UpdateAsync(SeedweaningDto dto)
        {
            var res = await _seedbirthRepository.FindAsync(dto.Id);
            if (res == null)
            {
                throw new UserFriendlyException("没有找到对应的用户");
            }
            ObjectMapper.Map(dto, res);
            var count = await _seedbirthRepository.UpdateAsync(res);
            var result = ObjectMapper.Map<Seedweaning, SeedweaningDto>(count);

            if (result != null)
            {
                return new EarResult<SeedweaningDto>() { Code = 200, Message = "修改成功！" };
            }
            else
            {
                return new EarResult<SeedweaningDto>() { Code = 500, Message = "修改失败！" };
            }
        }
    }
}
